<?php
namespace Admin\Controller;

//通用Controller类
class CommonController extends \Think\Controller{
   
    protected $customer;
    protected $customer_id;
    protected $where;
    protected $pagesize;
    protected $order;
    protected $Model;
    
    public function _initialize(){
        header("Content-Type: text/html; charset=utf-8");
        //判断是否登录
        $customer_info = session(C('ADMIN_SESSION'));
        if(empty($customer_info)) $this->toLogin();
            
        $this->customer_id = $customer_info['id'];
        $this->customer = D('Customer')->where("id='{$this->customer_id}'")->find();
        $this->assign('customer',$this->customer);
    }
    
    public function index($con_name = ''){
    	$this->where 	|| $this->where = array();
    	$this->pagesize || $this->pagesize = 10;
    	$this->order 	|| $this->order = 'id asc';
        $con_name || $con_name = CONTROLLER_NAME;
    	$this->Model 	|| $this->Model = D($con_name);
        $dataPage = $this->Model->getPager($this->where,$this->pagesize, $this->order);
        $this->assign('list',$dataPage['data']);
        $this->assign('page',$dataPage['page']);
        $this->display();
    }
    
    public function save(){
        $currentModel = D(CONTROLLER_NAME);
        if(IS_POST){
            $data = I('post.');
            if(!$currentModel->create($data['info'])) $this->error($currentModel->getError());
            $res = $currentModel->saveData($data['info']);
            $res === false ? $this->error('保存失败') : $this->success('保存成功');
        }else{
            $item = array();
            if(I('get.id')) $item = $currentModel->find(I('get.id'));
            $this->assign('item',$item);
            $this->assign('upPageUrl',$_SERVER['HTTP_REFERER']);
            $this->display();
        }
    }

    public function del(){
        $res = D(CONTROLLER_NAME)->delData(intval(I('request.id')));
        $res ? $this->success('删除成功') : $this->error('删除失败');
    }
    protected function toLogin(){
    	exit('<script type="text/javascript">
				if(window.top==window.self){
					window.location.href="'.U('Admin/Index/login').'";
				}else{
					parent.window.location.href="'.U('Admin/Index/login').'";
				}
			  </script>');
    }
}
